<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }
        div {
            border: 1px solid mediumslateblue;
            margin: 0 auto;
            height: 2000px;
            width: 1200px;
            background-color: mediumslateblue;
        }
        .top {
            background-color: lawngreen;
            width: 50px;
            height: 100px;
            position: fixed;
            right: 110px;
            top: 200px;
        }
        div section {
            position: absolute;
            width: 500px;
            height: 50px;
            left: 50%;
            top: 100px;
            transform: translateX(-50%);
            background-color: magenta;
        }
    </style>
</head>
<body>
    <div>
        <section>

        </section>
    </div>
    <div class="top">

    </div>
    <script>
        var secEle = document.querySelector("section");
        var topEle = document.querySelector(".top");
        window.onscroll = function (){
                var hei = parseInt(document.documentElement.scrollTop) ;
                if ( hei >= 100 ){
                    secEle.style.position = "fixed" ;
                    secEle.style.top = "0px" ; 
                } else {
                    secEle.style.position = "absolute" ;
                    secEle.style.top = "100px" ; 
                }
            
        }
    </script>
</body>
</html>